// 碳排子级占比排名
var numberFormate = iot.getNumberFormat("0.00");
// var subLevelDevMap = {
//     713 : {
//         ele : 697,
//         gas : 567
//     }
// }
dev.action = function(resp,param){
    var spaces = param.workSpaceName.split('/');
    var factoryName = spaces[spaces.length-1]
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    // var factoryId = '';

    var db = iot.db();
    // var resultFactory = db.qry("select es.id,es.factory,es.name from energy_struc es where es.pid = " + workSpace + " and es.type !='模板'");
    // var factory = iot.list();
    // resultFactory.forAll(function(item){
    //     factory.add(item.name);
    //     factoryId = factoryId + item.id + ","
    // });
    // factoryId = factoryId + 0;
    // var timeSub = parseInt(theTime.substring(0,4));

    var resultChildRank = null;
    // var electricityResult = null;
    // var gasResult = null;
    // if(timeSub >= 2019) {

    resultChildRank = db.qry("select es.name factory, es.factory as f, SUM(win.dvalue) totalSum \n" +
        "from iot_dev_data_win win\n" +
        "LEFT JOIN energy_struc es on es.pid = "+ workSpace +"\n" +
        "LEFT JOIN energy_struc_prop esp on esp.energy_struc_id = es.id\n" +
        "where 1=1\n" +
        "and win.dev_id = esp.dev_id\n" +
        "and win.data_type = esp.name\n" +
        "and win.the_time like '"+ theTime +"%'\n" +
        "GROUP BY win.dev_id;");
    /*}else{
        subLevelDevMap = getFactoryParam(factoryName, "碳排")
        if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].ele) {
            electricityResult = db.qry("select esp.energy_struc_id, es.name factory, SUM(dev_win.dvalue) totalSum " +
                " from iot_dev_data_win dev_win," +
                " (select * from energy_struc_prop e where type = 1 and name = '_用量' and energy_struc_id in (select es.id from energy_struc es where es.pid = "+ subLevelDevMap[workSpace].ele +" and es.name != '其他')) esp" +
                " LEFT JOIN energy_struc es" +
                " on esp.energy_struc_id = es.id" +
                " where dev_win.dev_id = esp.dev_id" +
                " and dev_win.data_type = esp.name" +
                " and dev_win.the_time like '" + theTime + "%'" +
                " GROUP BY energy_struc_id");
        }
        if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].gas) {
            gasResult = db.qry("select esp.energy_struc_id, es.name factory, SUM(dev_win.dvalue) totalSum " +
                " from iot_dev_data_win dev_win," +
                " (select * from energy_struc_prop e where type = 1" +
                " -- and name = '用量'\n" +
                " and energy_struc_id in (select es.id from energy_struc es where es.pid = "+ subLevelDevMap[workSpace].gas +" and es.name != '其他')) esp" +
                " LEFT JOIN energy_struc es" +
                " on esp.energy_struc_id = es.id" +
                " where dev_win.dev_id = esp.dev_id" +
                " and dev_win.data_type = esp.name" +
                " and dev_win.the_time like '" + theTime + "%'" +
                " GROUP BY energy_struc_id");
        }
    }*/
    var totalYield = 0
    var pvYield;
    //获取产量
    // if (factoryName !== 'PV') {
    //     var yieldId = getFactoryYieldId(factoryName)
    //
    //     var yieldResult = getFactoryYield(yieldId, theTime)
    //
    //     totalYield = yieldResult.totalSum;
    // }else {
    //     pvYield = db.qry("select SUM(dvalue) totalSum, dev_id devId from iot_dev_data_win\n" +
    //         "where dev_id in (278, 11024, 11129, 11201, 11165)\n" +
    //         "and the_time like '"+ theTime +"%' \n" +
    //         "GROUP BY devId");
    // }

    var datas = iot.list();
    var legend = iot.list();
    resultChildRank.forAll(function (item) {
        var sum = 0;
        sum = item.totalSum;
        //判断当前选择总数还是单车
        // if (totalOrSingle.equals("total")) {
        datas.add({name: item.factory, value: numberFormate.format(sum)});
        legend.add(item.factory);
        // }else if (totalOrSingle.equals("single")) {
        //     if (factoryName !== 'PV') {
        //         if (sum && totalYield && totalYield !== 0) {
        //             sum = sum / totalYield;
        //         }
        //         datas.add({name: item.factory, value: numberFormate.format(totalYield === 0 ? 0 : sum)});
        //         legend.add(item.factory);
        //     }else {
        //         var yieldNum = 0;
        //         pvYield.forAll(function (yieldItem) {
        //             if (factoryYield[item.f] === yieldItem.devId){
        //                 yieldNum = yieldItem.totalSum;
        //             }
        //         })
        //         if (sum && yieldNum && yieldNum !== 0) {
        //             sum = sum / yieldNum;
        //         }
        //         datas.add({name: item.factory, value: numberFormate.format(yieldNum === 0 ? 0 : sum)});
        //         legend.add(item.factory);
        //     }
        // }
    });
    // for (var index in factory) {
    //     var sum = 0;
        // if(timeSub >= 2019) {
        //     resultChildRank.forAll(function (item) {
        //         if (item && item.factory && item.factory === factory[index]) {
        //             sum = item.totalSum;
        //         }
        //     });
        // }
        // else{
        //     //获取工厂名称
        //     var factoryName = getFactoryName(param.workSpaceName);
        //     electricityResult.forAll(function (item) {
        //         if (item && item.factory && item.factory === factory[index]) {
        //             sum = item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].ele : 0);
        //         }
        //     });
        //     gasResult.forAll(function (item) {
        //         if (item && item.factory && item.factory === factory[index]) {
        //             sum = sum + item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].gas : 0);
        //         }
        //     });
        // }

        // //判断当前选择总数还是单车
        // if (totalOrSingle.equals("total")) {
        //     datas.add({name: factory[index], value: numberFormate.format(sum)});
        //     legend.add(factory[index]);
        // }else if (totalOrSingle.equals("single")) {
        //     if (sum && totalYield){
        //         sum = sum/totalYield;
        //     }
        //     datas.add({name: factory[index], value: numberFormate.format(totalYield === null ? 0 : sum)});
        //     legend.add(factory[index]);
        // }
    // }

    var jsData = {
        datas : datas,
        legend: legend
    };
    resp.setJsData(jsData);
}